<template>
  <div>
    <el-tree :props="props" :load="loadNode" node-key="id" :default-expanded-keys="defaultExpandedKeys" lazy
      @node-expand="handleNodeExpand" />
  </div>
</template>

<script>
  import {
    listBracher
  } from "network/brach/BranchRequest.js"
  export default {
    name: "BranchTree",
    data() {
      return {
        props: {
          label: 'name',
          id: 'id',
          isLeaf: 'nodeIsLeaf',
          children: 'children'
        },
        defaultExpandedKeys: [],
        count: 1
      };
    },
    methods: {
      handleNodeExpand(data) {
        console.log("handleNodeExpand：");
        console.log(data);
      },
      //处理网点请求返回数据
      handleData(res){
         if(res.data != null && res.data.length > 0){
           res.data.forEach(item => {
             item.nodeIsLeaf = item.is_leaf != null && item.is_leaf == 1
           })
         }
         return res.data;
      },
      loadNode(node, resolve) {
        //获取顶层节点
        if (node.level === 0) {
          listBracher(null, null)
            .then(res => this.handleData(res))
            .then(data => {
              this.defaultExpandedKeys.push(data[0].id);
              resolve(data);
            })
            .catch(err => {
              console.log(err);
            })
        } else {
          //非顶层节点
          listBracher(node.data.id, 1)
            .then(res => this.handleData(res))
            .then(data => resolve(data))
            .catch(err => console.log(err))
        }
      }
    }
  }
</script>

<style>
</style>
